/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */

package ant.antsys;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * This class implemented TestListerner to track the progress duriing test
 * @author johnson ma
 * @see TestListener
 * @see TestSuiteResult
 */

public class TestMonitor implements TestListener {

	private static final String REPORT_PREFIX = "_report.xml";

	private int numOfTestCase = 0;
	private int numOfFailed = 0;
	private int numOfRun = 0;

	private transient long suiteStartTime;
	private transient long caseStartTime;

	private boolean curCaseSucceed;
	private String curCaseError;
	TestSuiteResult result;

	public void startTestCase(TestCase t) {
		numOfTestCase++;
		numOfRun++;
		//        curCase = t.getName();
		curCaseSucceed = true;
		curCaseError = null;
		caseStartTime = System.currentTimeMillis();
	};

	public void endTestCase(TestCase t) {
		long caseEndTime = System.currentTimeMillis();
		float duration = (int) (caseEndTime - caseStartTime) / 100;
		result.addTestCaseResult(t.getName(), duration, curCaseSucceed,
				curCaseError);
		if (!curCaseSucceed) {
			numOfFailed++;
		}
	}

	public void addFailuer(TestCase t, String errorMsg, String location) {
		curCaseSucceed = false;
		curCaseError = errorMsg;
	}

	public void startTestSuite(TestSuite t) {
		suiteStartTime = System.currentTimeMillis();
		result = new TestSuiteResult(t.getName());

	}

	public void endTestSuite(TestSuite t) {
		long suiteEndTime = System.currentTimeMillis();
		float duration = (int) (suiteEndTime - suiteStartTime) / 100;
		result.setDuration(duration);
		result.setNumOfTestCase(numOfTestCase);
		result.setNumOfRun(numOfRun);
		result.setNumOfFailed(numOfFailed);
		printResult(result);
		if (t.getHtmlReport()) {
			generateReport(t);
		}

	}

	private void generateReport(TestSuite t) {
		FileOutputStream fo = null;
		try {
			File outputPath = new File(t.getToDir());
			if (!outputPath.exists()) {
				outputPath.mkdirs();
			}
			//generate xml report
			File xmlReportFile = new File(outputPath, t.getName()
					+ REPORT_PREFIX);
			fo = new FileOutputStream(xmlReportFile);
			XMLReport xmlResult = new XMLReport();
			xmlResult.setOutput(fo);
			xmlResult.report(result);
			//generate html report
			HtmlReport report = new HtmlReport(t);
			report.setToDir(outputPath);
			report.setXmlFile(xmlReportFile);
			report.execute();

		} catch (Exception e) {
			System.err.println("error when generate report:" + e);
			e.printStackTrace();
		} finally {
			if (fo != null) {
				try {
					fo.close();
				} catch (IOException io) {
				}
			}
		}
	}

	protected void printResult(TestSuiteResult result) {
		System.out.println("TestSuite:" + result.getTestSuiteName()
				+ ", Tests run:" + result.getNumOfTestCase() + ", Errors:"
				+ result.getNumOfFailed() + ", Time elapsed: "
				+ result.getDuration() + " sec");
	}
}
